package com.bdqn.controller;

import com.bdqn.pojo.DevUser;
import com.bdqn.service.DevUserService;
import com.bdqn.util.Constants;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/dev")
public class IndexController {
    @Resource
    private DevUserService devUserService;

    @GetMapping("/login")
    public String toLogin(){
        return "devlogin";
    }
    @PostMapping("/doLogin")
    public String doLogin(HttpSession session, HttpServletRequest request, @RequestParam("devCode") String devCode, @RequestParam("devPassword")String devPassword){
        DevUser login = devUserService.login(devCode, devPassword);
        if(login == null){
            request.setAttribute("error", "用户名或密码错误");
            return "redirect:/dev/login";
        }
        session.setAttribute(Constants.devUserSession, login);
        return "developer/main";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute(Constants.devUserSession);
        session.invalidate();
        return "redirect:/dev/login";
    }

}
